<template>
  <el-card>
    <el-table border style="margin: 10px 0" :data="skuArr">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="150px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="150px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" width="150px">
        <template #default="{ row }">
          <el-image
            :src="row.skuDefaultImg"
            style="width: 100px; height: 100px"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[row.skuDefaultImg]"
            fit="cover"
            :preview-teleported="true"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        width="150px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        width="150px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="updateOnSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="updateSku"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="findSku(row)"
          ></el-button>
          <el-popconfirm
            title="`Are you sure to delete ${row.skuName}?`"
            width="200px"
            @confirm="deleteSku(row.id)"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getHasSku"
    />
    <!-- 抽屉组件 展示商品详情 -->
    <el-drawer v-model="drawer">
      <!--      标题部分-->
      <template #header>
        <h4>查看商品的详情</h4>
      </template>
      <template #default>
        <el-row class="layout-row">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row class="layout-row">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row class="layout-row">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row class="layout-row">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 0 5px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row class="layout-row">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 0 5px"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row class="layout-row">
          <!--          <el-col :span="6">商品图片</el-col>-->
          <!--          <el-col :span="18">-->
          <el-col :span="24">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt="商品图片"
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
      <!--      <template #footer>-->
      <!--        <div style="flex: auto">-->
      <!--          <el-button @click="cancelClick">cancel</el-button>-->
      <!--          <el-button type="primary" @click="confirmClick">confirm</el-button>-->
      <!--        </div>-->
      <!--      </template>-->
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import {
  reqCancelSale,
  reqDeleteSku,
  reqOnSale,
  reqSkuInfo,
  reqSkuList,
} from '@/api/product/sku'
import { SkuData, SkuInfo, SkuResponseData } from '@/api/product/sku/type.ts'
import { ElMessage } from 'element-plus'
// 分页器当前页码
let pageNo = ref<number>(1)
// 每一页展示几条数据
let pageSize = ref<number>(10)
const total = ref<number>(0)
let skuArr = ref<SkuData[]>([])

// 组件挂载完毕
onMounted(() => {
  getHasSku()
})
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code == 200) {
    total.value = result?.data.total || 0 // total没有给默认值 https://blog.csdn.net/qq_29517595/article/details/134668408
    skuArr.value = result.data.records
  }
}
// 分页器下拉菜单发生变化触发
// const handleSizeChange = (size: number) => {
const handleSizeChange = () => {
  // console.log(size)
  getHasSku()
}

const updateOnSale = async (row: SkuData) => {
  // 如果当前商品的isSale==1更新为下架
  if (row.isSale == 1) {
    await reqCancelSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
    // 发请求获取更新完毕的全部已有SKU
    await getHasSku(pageNo.value)
  } else {
    await reqOnSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '上架成功',
    })
    // 发请求获取更新完毕的全部已有SKU
    await getHasSku(pageNo.value)
  }
}

const updateSku = () => {
  ElMessage({
    type: 'info',
    message: '暂未开发...',
  })
}

// 控制抽屉显示与隐藏
let drawer = ref<boolean>(false)
// 抽屉打开的方向
// let direction = ref<string>('rtl')
let skuInfo = ref<SkuData>({})
const findSku = async (row: SkuData) => {
  // 抽屉展示出来
  drawer.value = true
  // 获取已有商品详情数据
  let result: SkuInfo = await reqSkuInfo(row.id as number)
  skuInfo.value = result.data
}
// 删除已有的一个商品
const deleteSku = async (row: SkuData) => {
  let result: any = await reqDeleteSku(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    // 获取删除完毕的全部已有SKU
    // 如果当前页还有数据就留在当前页，如果没有数据就回到上一页
    await getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    // 删除失败
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>
<style scoped lang="scss">
.layout-row {
  margin: 10px 0;
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
